/**
 * @author rxliuli
 */
const {not} = rx.CombinedPredicate,
    {isFloat} = rx.stringValidator
const notFloat = not(isFloat)
const app = new BaseValidateVue({
    el: '#app',
    data() {
        return {
            form: this.createForm(),
            houseMap: new Map(),
            ruleList: [
                new ValidateObject({
                    ruleName: 'checkTransientFloat',
                    validate: this.transientFloatValidate,
                }),
                new ValidateObject({
                    ruleName: 'checkTransientMax',
                    validate: this.transientMaxValidate,
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.ruleList;
        },
        createForm() {
            return new BaseMonitorDevice({
                houseId: '',
                appState: '',
                connectType: 1,
                linkStatus: 1,
            })
        },
        async transientMaxValidate(val, args) {
            const max = en => en.split('.')[0]
            return max(val).length <= 11
        },
        async transientFloatValidate(val, args) {
            return !notFloat(val)
        },
        async add() {
            this.$validator.validateAll().then(async (res) => {
                if (res) {
                    if (await ajaxOption({
                        res: baseMonitorDeviceApi.add(this.form),
                        danger: '添加失败',
                        success: '添加成功，即将返回到列表页面',
                    })) {
                        await rx.wait(1000)
                        forward('/basic-data/admin_monitor_device')
                        /* history.back() */
                    }
                } else {
                    rxPrompt.dangerMsg("输入存在错误！");
                }
            })
        },
        async reset() {
            this.form = this.createForm()
        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
    async mounted() {
        this.houseMap = rx.arrayToMap(await baseHouseInfoApi.selectList(), ({id}) => id, ({houseName}) => houseName)
    },
})